package com.yy.jhl.ui.user.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnDoubleClickListener;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.ui.user.contract.BindPhoneContract;
import com.yy.jhl.ui.user.presenter.BindPhonePresenter;
import com.yy.jhl.util.DataUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by libingn on 2017/12/4.
 */

public class FillCodeActivity extends BaseActivity<BindPhonePresenter> implements BindPhoneContract.View {


    @BindView(R.id.phone_num)
    TextView phoneNum;
    @BindView(R.id.code_edit)
    EditText codeEdit;
    @BindView(R.id.again_get_code)
    TextView againGetCode;
    @BindView(R.id.ensure_btn)
    Button ensureBtn;
    private int currentType = 1;//1:发送验证码 2：校验
    @Override
    public int getLayoutId() {
        return R.layout.act_fill_code;
    }

    @Override
    public void initView() {
        ensureBtn.setClickable(false);
        showTitle("填写验证码", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        String currentPhone = DataUtil.getUserInfo(mContext).getMobile_phone();
        phoneNum.setText(StringUtil.isEmpty(currentPhone) ? "" : "请输入"+StringUtil.getPhoneMiddle(currentPhone)+"收到的手机验证码");
        codeEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String phone = charSequence.toString();
                if (!StringUtil.isEmpty(phone)){
                    ensureBtn.setClickable(true);
                    ensureBtn.setBackgroundResource(R.drawable.green_btn_bg);
                }else {
                    ensureBtn.setClickable(false);
                    ensureBtn.setBackgroundResource(R.drawable.gray_btn_bg);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        sendCode();
    }
    private void sendCode(){
        String currentPhone = DataUtil.getUserInfo(mContext).getMobile_phone();
        mPresenter.bindPhone("1",currentPhone,"");
    }
    private void verifyCode(String code){
        String currentPhone = DataUtil.getUserInfo(mContext).getMobile_phone();
        mPresenter.bindPhone("1",currentPhone,code);
    }

    @OnClick({R.id.again_get_code, R.id.ensure_btn})
    public void onViewClicked(View view) {
        String code = codeEdit.getText().toString().trim();
        switch (view.getId()) {
            case R.id.again_get_code:
                currentType = 1;
                sendCode();
                break;
            case R.id.ensure_btn:
                if (StringUtil.isEmpty(code)){
                    showShortToast("验证码不能为空");
                    return;
                }
                currentType = 2;
                verifyCode(code);
                break;
        }
    }

    @Override
    public void bindSuccess() {
        if (currentType == 1){
            showShortToast("验证码发送成功");
        }else {
            startActivity(NextBindPhoneActivity.class);
            finish();
        }
    }

    @Override
    public void bindFail() {

    }
}
